package org.oschina.api.user.user.controller;

import org.apache.commons.lang.StringUtils;
import org.oschina.api.BaseController;
import org.oschina.api.user.user.model.User;
import org.oschina.api.user.user.service.UserService;
import org.oschina.api.user.user.vo.UserHomeAccessVo;
import org.oschina.api.user.user.vo.UserMessageVo;
import org.oschina.api.user.user.vo.UserSearchVo;
import org.oschina.api.user.user.vo.UserVo;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.EmailFilter;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.oschina.handler.validator.filter.UploadFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(Config.API_PATH+"/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;
	
	/**
	 * 认证
	 * @param identity
	 * @return
	 */
	@Validate
	@RequestMapping(value="/register/check",method=RequestMethod.POST)
	public Result registerCheck
	(
		@Validate(validateField=@ValidateField()) String identity
	)
	{
		return userService.registerCheck(identity);
	}
	
	/**
	 * 更新密码
	 * @param oldPassword
	 * @param newPassword
	 * @return
	 */
	@Validate
	@RequestMapping(value="/password/update",method=RequestMethod.POST)
	public Result updatePassword
	(
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class},validateValue=true,min=5,max=20)) String oldPassword,
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class},validateValue=true,min=5,max=20)) String newPassword
	)
	{
		return userService.updatePassword(oldPassword, newPassword);
	}
	
	/**
	 * 搜索
	 * @param account
	 * @param username
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/search",method=RequestMethod.GET)
	public Pager<UserSearchVo> search(String account,String username,Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		if(StringUtils.isBlank(account))
			account="";
		
		if(StringUtils.isBlank(username))
			username="";
		
		return userService.search(account, username, pn);
	}
	
	/**
	 * 更新角色
	 * @param id
	 * @param role
	 * @return
	 */
	@Validate
	@RequestMapping(value="/{id}/role/update",method=RequestMethod.POST)
	public Result updateRole
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer role
	)
	{
		return userService.updateRole(id, role);
	}
	
	/**
	 * 踢下线
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/kick",method=RequestMethod.POST)
	public Result kick
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return userService.kick(id);
	}
	
	/**
	 * 编辑
	 * @param user
	 * @return
	 */
	@Validate
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public Result edit
	(
		@Validate(validateField=
		{
			@ValidateField(name="headphoto",filter={TextOnlyFilter.class,UploadFilter.class}),
			@ValidateField(name="signature",filter={TextOnlyFilter.class}),
			@ValidateField(name="username",filter={TextOnlyFilter.class})
		}) User user
	)
	{
		return userService.edit(user);
	}
	
	/**
	 * 注册
	 * @param account
	 * @param password
	 * @return
	 */
	@Validate
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public Result register
	(
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class,EmailFilter.class},validateValue=true,min=5,max=20)) String account,
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class},validateValue=true,min=5,max=20)) String password
	)
	{
		return userService.register(account, password);
	}

	/**
	 * 登录
	 * @param account
	 * @param password
	 * @return
	 */
	@Validate
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public Result login
	(
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class,EmailFilter.class},validateValue=true,min=5,max=20)) String account,
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class},validateValue=true,min=5,max=20)) String password
	)
	{
		return userService.login(account, password);
	}
	
	/**
	 * 登出
	 * @return
	 */
	@RequestMapping(value="/logout",method=RequestMethod.POST)
	public Result logout()
	{
		return userService.logout();
	}
	
	/**
	 * 根据token获得用户信息
	 * @return
	 */
	@RequestMapping(value="/message/token",method=RequestMethod.GET)
	public UserVo getMessageByToken()
	{
		return userService.getMessageByToken();
	}
	
	/**
	 * 根据ID获得用户信息
	 * @return
	 */
	@Validate
	@RequestMapping(value="/message/{id}",method=RequestMethod.GET)
	public UserMessageVo getMessageById
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return userService.getMessageById(id);
	}
	
	/**
	 * 首页入口
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/home/{id}",method=RequestMethod.GET)
	public UserHomeAccessVo homeAccess
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return userService.homeAccess(id);
	}
}